import request from './request/index.js'
import router from '../router/index.js'
import {ElMessage} from "element-plus";

export async function login(data) {
    try {
        // 假设后端返回结构为 { code: 200, data: { token: '...' }, message: '...' }
        const res = await request.post('/user/login', data)
        const token = res.data
        if (token) {
            localStorage.setItem('Yang-ERP-jwt', token)
            ElMessage.success('登录成功')
            await router.push('/home')
        }
        return res.data
    } catch (err) {
        return Promise.reject(err)
    }
}

export async function getUserInfo() {
    try {
        const res = await request.get('/user/info')
        return res.data
    } catch (err) {
        await router.push('/login')
        return Promise.reject(err)
    }
}